﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class InvHoldDA : IInvHoldDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string HOLD_BY = "@HOLD_BY";
        private string HOLD_DATE = "@HOLD_DATE";
        private string HOLD_REASON_CODE = "@HOLD_REASON_CODE";
        private string HOLD_REASON_DESC = "@HOLD_REASON_DESC";
        private string HOLD_TYPE = "@HOLD_TYPE";
        private string INV_HOLD_ID = "@INV_HOLD_ID";
        private string IS_HOLD = "@IS_HOLD";
        private string LOC = "@LOC";
        private string LOT = "@LOT";
        private string LPN_ID = "@LPN_ID";
        private string OWNER_ID = "@OWNER_ID";
        private string RELEASED_BY = "@RELEASED_BY";
        private string RELEASED_DATE = "@RELEASED_DATE";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_INV_HOLD_ID_UNIQUE = " SELECT COUNT(1) FROM @_@INV_HOLD WHERE INV_HOLD_ID = @INV_HOLD_ID ";
        private string SQL_DELETE_INV_HOLD = " DELETE FROM @_@INV_HOLD WHERE INV_HOLD_ID = @INV_HOLD_ID ";
        private string SQL_INSERT_INV_HOLD = " INSERT INTO @_@INV_HOLD ( WH_ID, INV_HOLD_ID, LOT, LPN_ID, LOC, OWNER_ID, SKU_ID, IS_HOLD, HOLD_TYPE, HOLD_REASON_CODE, HOLD_REASON_DESC, HOLD_BY, HOLD_DATE, RELEASED_BY, RELEASED_DATE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @INV_HOLD_ID, @LOT, @LPN_ID, @LOC, @OWNER_ID, @SKU_ID, @IS_HOLD, @HOLD_TYPE, @HOLD_REASON_CODE, @HOLD_REASON_DESC, @HOLD_BY, @HOLD_DATE, @RELEASED_BY, @RELEASED_DATE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_INV_HOLD = " SELECT  ROWID, WH_ID, INV_HOLD_ID, LOT, LPN_ID, LOC, OWNER_ID, SKU_ID, IS_HOLD, HOLD_TYPE, HOLD_REASON_CODE, HOLD_REASON_DESC, HOLD_BY, HOLD_DATE, RELEASED_BY, RELEASED_DATE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@INV_HOLD ";
        private string SQL_UPDATE_INV_HOLD = " UPDATE @_@INV_HOLD SET WH_ID = @WH_ID, LOT = @LOT, LPN_ID = @LPN_ID, LOC = @LOC, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, IS_HOLD = @IS_HOLD, HOLD_TYPE = @HOLD_TYPE, HOLD_REASON_CODE = @HOLD_REASON_CODE, HOLD_REASON_DESC = @HOLD_REASON_DESC, HOLD_BY = @HOLD_BY, HOLD_DATE = @HOLD_DATE, RELEASED_BY = @RELEASED_BY, RELEASED_DATE = @RELEASED_DATE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE INV_HOLD_ID = @INV_HOLD_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckInvHoldIDUnique(string invHoldID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invHoldID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_INV_HOLD_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckInvHoldIDUnique(DataBase dataBase, DbTransaction tran, string invHoldID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invHoldID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_INV_HOLD_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteInvHold(List<string> invHoldID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invHoldID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invHoldID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INV_HOLD.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteInvHold(DataBase dataBase, DbTransaction tran, List<string> invHoldID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invHoldID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invHoldID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INV_HOLD.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllInvHold(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllInvHold(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public InvHoldInfo GetInvHoldByID(string invholdID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID) + " WHERE INV_HOLD_ID = @INV_HOLD_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invholdID) };
            InvHoldInfo invHoldInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    invHoldInfo = new InvHoldInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INV_HOLD_ID"].ToString(), reader["LOT"].ToString(), reader["LPN_ID"].ToString(), reader["LOC"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["IS_HOLD"].ToString(), reader["HOLD_TYPE"].ToString(), reader["HOLD_REASON_CODE"].ToString(), reader["HOLD_REASON_DESC"].ToString(), reader["HOLD_BY"].ToString(), (reader["HOLD_DATE"].ToString() != "") ? DateTime.Parse(reader["HOLD_DATE"].ToString()) : (CS00003 = new DateTime()), reader["RELEASED_BY"].ToString(), (reader["RELEASED_DATE"].ToString() != "") ? DateTime.Parse(reader["RELEASED_DATE"].ToString()) : (CS00003 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return invHoldInfo;
        }

        public InvHoldInfo GetInvHoldByID(DataBase dataBase, DbTransaction tran, string invholdID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID) + " WHERE INV_HOLD_ID = @INV_HOLD_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INV_HOLD_ID", invholdID) };
            InvHoldInfo invHoldInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                invHoldInfo = new InvHoldInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INV_HOLD_ID"].ToString(), reader["LOT"].ToString(), reader["LPN_ID"].ToString(), reader["LOC"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["IS_HOLD"].ToString(), reader["HOLD_TYPE"].ToString(), reader["HOLD_REASON_CODE"].ToString(), reader["HOLD_REASON_DESC"].ToString(), reader["HOLD_BY"].ToString(), (reader["HOLD_DATE"].ToString() != "") ? DateTime.Parse(reader["HOLD_DATE"].ToString()) : (CS00003 = new DateTime()), reader["RELEASED_BY"].ToString(), (reader["RELEASED_DATE"].ToString() != "") ? DateTime.Parse(reader["RELEASED_DATE"].ToString()) : (CS00003 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return invHoldInfo;
        }

        public DataSet GetInvHoldByQueryList(List<string> sqlWhere, InvHoldQueryEntity invholdQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!invholdQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, invholdQuery.CurrentPage, invholdQuery.PageSize, invholdQuery.SortField, invholdQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetInvHoldByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, InvHoldQueryEntity invholdQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_INV_HOLD.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!invholdQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, invholdQuery.CurrentPage, invholdQuery.PageSize, invholdQuery.SortField, invholdQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertInvHold(InvHoldInfo invHoldInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                invHoldInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_InvHold_Parameters(invHoldInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_INV_HOLD.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertInvHold(DataBase dataBase, DbTransaction tran, InvHoldInfo invHoldInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                invHoldInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_InvHold_Parameters(invHoldInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_INV_HOLD.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_InvHold_Parameters(InvHoldInfo invHoldInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.INV_HOLD_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOT, SqlDbType.VarChar, 20), new SqlParameter(this.LPN_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOC, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.IS_HOLD, SqlDbType.VarChar, 1), new SqlParameter(this.HOLD_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.HOLD_REASON_CODE, SqlDbType.VarChar, 20), new SqlParameter(this.HOLD_REASON_DESC, SqlDbType.VarChar, 50), new SqlParameter(this.HOLD_BY, SqlDbType.VarChar, 20), new SqlParameter(this.HOLD_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.RELEASED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.RELEASED_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), 
                new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(invHoldInfo.WhID))
            {
                paramArray[0].Value = invHoldInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = invHoldInfo.InvHoldID;
            paramArray[2].Value = invHoldInfo.Lot;
            paramArray[3].Value = invHoldInfo.LpnID;
            paramArray[4].Value = invHoldInfo.Loc;
            paramArray[5].Value = invHoldInfo.OwnerID;
            paramArray[6].Value = invHoldInfo.SkuID;
            paramArray[7].Value = invHoldInfo.IsHold;
            paramArray[8].Value = invHoldInfo.HoldType;
            paramArray[9].Value = invHoldInfo.HoldReasonCode;
            if (!string.IsNullOrEmpty(invHoldInfo.HoldReasonDesc))
            {
                paramArray[10].Value = invHoldInfo.HoldReasonDesc;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            paramArray[11].Value = invHoldInfo.HoldBy;
            paramArray[12].Value = invHoldInfo.HoldDate;
            paramArray[13].Value = invHoldInfo.ReleasedBy;
            paramArray[14].Value = invHoldInfo.ReleasedDate;
            paramArray[15].Value = invHoldInfo.CreatedBy;
            paramArray[0x10].Value = invHoldInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateInvHold(InvHoldInfo invHoldInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    invHoldInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_InvHold_Parameters(invHoldInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_INV_HOLD.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateInvHold(DataBase dataBase, DbTransaction tran, InvHoldInfo invHoldInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                invHoldInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_InvHold_Parameters(invHoldInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_INV_HOLD.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

